package com.fita.admin.handler.impl;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.fita.admin.handler.EhiHandler;
import com.fita.admin.rao.EhiRao;
import com.fita.entity.dto.ehi.response.VehicleReservationCancleResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
/**
 * 
 * @author sjh
 *
 */

@Component("ehiHandler")
public class EhiHandlerImpl implements EhiHandler{

	private static final Logger logger = LoggerFactory.getLogger(EhiHandlerImpl.class);
	
	@Value("#{config['api.host']}")
    private String apiHost;
    
    @Value("#{config['api.mock']}")
    private boolean apiMock;
    
    @Rao
    private EhiRao ehiRao;
	
	/*
	 * (non-Javadoc)
	 * @see com.fita.admin.handler.EhiHandler#vehicleReservationCancle(java.util.Map)
	 */
	public VehicleReservationCancleResponse vehicleReservationCancle(Map<String, Object> params) {
		logger.info("start call rao with params [{}]", JsonUtils.toJson(params));
		
	    HttpResult result = ehiRao.vehicleReservationCancle(apiHost + "/api/vehicles/cancle-reservation?mock=" + apiMock, params);
	        
        logger.info("call rao success with result [{}]", JsonUtils.toJson(result));
        
        if (!result.isValid()) {
        	
        	throw BusinessException.newInstance(result.getCode(), result.getMessage());
        }		
        String responseString = JsonUtils.toJson(result.get("response"));
        
        VehicleReservationCancleResponse vehicleReservationCancleResponse = JsonUtils.parse(responseString, VehicleReservationCancleResponse.class);
        
        return vehicleReservationCancleResponse;
	}

}
